﻿// Example header text. Can be configured in the options.
using System;
using System.Configuration;
using System.Management;
using System.Windows.Forms;
using vtech_frameWorks.Exceptions;

namespace vtech_frameWorks.Library
{
    public class ClsUtilities
    {
        /// <summary>
        /// Lấy giá trị mặc dc được thiết lập trong file config
        /// </summary>
        /// <param name="msgConstantKey"></param>
        /// <returns></returns>
        public static string GetConfigKey(string msgConstantKey)
        {
            string msg;
            try
            {
                msg = ConfigurationManager.AppSettings[msgConstantKey];
            }
            catch(Exception ex)
            {
                msg = string.Empty;
                ClsLogServices.WriteLog(LogLevel.ERROR, ex.Message);
            }
            return msg;
        }

        /// <summary>
        /// Gan giá trị mặc dc được thiết lập trong file config
        /// </summary>
        /// <param name="msgConstantKey"></param>
        /// <returns></returns>
        public static void SetConfigKey(string msgConstantKey,string msgConstantValue)
        {
            try
            {
                System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                config.AppSettings.Settings.Remove(msgConstantKey);
                config.AppSettings.Settings.Add(msgConstantKey,msgConstantValue);
                
            }
            catch (Exception ex)
            {
                ClsLogServices.WriteLog(LogLevel.ERROR, ex.Message);
            }
        }


        public static string GetMacAddressID()
        {
            string strMacAddress = ClsString.String;
            try
            {
                ManagementObjectCollection instances = new ManagementClass("Win32_NetworkAdapterConfiguration").GetInstances();
                foreach (ManagementObject objManagement in instances)
                {
                    if ((strMacAddress == string.Empty) && ((bool)objManagement["IPEnabled"]))
                    {
                        strMacAddress = objManagement["MacAddress"].ToString();
                    }
                    objManagement.Dispose();
                }
                strMacAddress = strMacAddress.Replace(":", "");
                
            }
            catch (Exception ex)
            {
                ClsInformation.Error(ex.Message);
                return null;
            }
            return strMacAddress;
        }

        public static void ShowHelp(string objectName)
        {
            try
            {
                string keyword = "";
                keyword = ConfigurationSettings.AppSettings[objectName];
                if (keyword == null)
                {
                    keyword = "./Help.htm";
                }
                string url = ConfigurationSettings.AppSettings["HelpFileName"];
                Help.ShowHelp(null, url, keyword);
            }
            catch (Exception)
            {
            }
        }

        public static string GetRootDirectory
        {
            get
            {
                return Application.StartupPath;
            }
        }
    }
}